<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>34个可以提升开发效率的JavaScript单行程序代码片段</title>
</head>

<body>

</body>
<script>
    // https://mp.weixin.qq.com/s/Y6uJFLoRbCfE9CD-x2aC-Q

    // 1. 比较两个数组
    // every 检测数组元素是否符合指定条件
    // `a` and `b` are arrays
    const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
    // Or
    // const isEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
    // Examples
    console.log(isEqual([1, 2, 3], [1, 2, 3])); // true
    console.log(isEqual([1, 2, 3], [1, '2', 3])); // false

    function isEqual2(a, b) {
        //   return JSON.stringify(a) === JSON.stringify(b)

        if (a.length === b.length) {
            return a.every((v, i) => {
                console.log('v === b[i]', v === b[i])
                return v === b[i]
            })
        }
    }

    // console.log(isEqual2([1, 2, 3], [1, 3, 2]))


    // 将对象数组转换为单个对象

    // const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});
    // Or
    const toObject = (arr, key) => Object.fromEntries(arr.map((it) => [it[key], it]));
    // Example

    console.log(toObject([
        { id: '1', name: 'Alpha', gender: 'Male' },
        { id: '2', name: 'Bravo', gender: 'Male' },
        { id: '3', name: 'Charlie', gender: 'Female' }],
        'id'))
    /*
        {
        '1': { id: '1', name: 'Alpha', gender: 'Male' },
        '2': { id: '2', name: 'Bravo', gender: 'Male' },
        '3': { id: '3', name: 'Charlie', gender: 'Female' }
        }
        */
    let arr = [
        { id: '1', name: 'Alpha', gender: 'Male' },
        { id: '2', name: 'Bravo', gender: 'Male' },
        { id: '3', name: 'Charlie', gender: 'Female' }]

    function toObject2(arr, key) {
        return arr.reduce((a, b, i) => {
            console.log('123456789', a, b, i)
            return ({
                ...a,
                [b[key]]: b,
            })
        }, {})
    }

    // console.log('toObject2 ', toObject2(arr, 'id'))
    // const toObject = (arr, key) => Object.fromEntries(arr.map((it) => [it[key], it]));
    function toObject3(arr, key) {
        let a = arr.map((it) => {
            return [
                it[key],
                it
            ]
        })
        // console.log('a',a)
        return Object.fromEntries(a)
    }

    // console.log('toObject3 ', toObject3(arr, 'id'))

    // 判断数组是否为空
    function isNotEmpty() {
        return Array.isArray(arr) && Object.keys(arr).length > 0
    }

    console.log(Object.keys(arr))


    /** 
     *   对象
     * 
     * **/

    // 检查多个对象是否相等

    const isEqual3 = (...objects) => objects.every((obj) => JSON.stringify(obj) === JSON.stringify(objects[0]));
    // Examples
    isEqual3({ foo: 'bar' }, { foo: 'bar' }); // true
    isEqual3({ foo: 'bar' }, { bar: 'foo' }); // false


    function isEqual2(...objects) {
        return objects.every(obj => {
            console.log(obj)
            return JSON.stringify(obj) === JSON.stringify(objects[0])
        })
    }
    console.log(isEqual2({ foo: 'bar' }, { foo: 'bar' }, { bar: 'foo' }))
    console.log(isEqual2({ foo: 'bar' }, { foo: 'bar' }, { foo: 'bar' }))

    // 删除对象中的null和未定义的属性

    const removeNullUndefined = (obj) =>
        Object.entries(obj)
            .reduce((a, [k, v]) => (v == null ? a : ((a[k] = v), a)), {});
    // Or
    // const removeNullUndefined = (obj) =>
    //     Object.entries(obj)
    //         .filter(([_, v]) => v != null)
    //         .reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
    // Or
    // const removeNullUndefined = (obj) => Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null));
    // Example
    let obj = {
        foo: null,
        bar: undefined,
        fuzz: 42
    }
    removeNullUndefined(obj);

    function removeNullUndefined2(obj) {
        // Object.entries(obj)
        Object.entries(obj).reduce((a, [k, y], i) => {
            console.log('reduce', 'a:', a, "k:", k, i)
            if (y == null) {
                return a
            } else {
                return (a[k] = y,a)
            }
        }, {})
    }
    removeNullUndefined2(obj)

// { fuzz: 42 }


</script>

</html>